﻿using System;
using System.Collections.Generic;
using System.Text;
using DevComponents.DotNetBar;
using System.Drawing;
using DevComponents.Editors;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace TextEditor.TextDocument.Control
{
    public class CustomDashStyleComboBox : DevComponents.DotNetBar.Controls.ComboBoxEx
    {
        public Type _type;

        public CustomDashStyleComboBox()
        {
            this.DropDownStyle = ComboBoxStyle.DropDownList;
            this._type = typeof(DashStyle);
            RefreshComboItem();
        }


        public CustomDashStyleComboBox(string _actionName)
        {
            this.DropDownStyle = ComboBoxStyle.DropDownList;
            this.Name = _actionName;
            this._type = typeof(DashStyle);
            RefreshComboItem();
        }

        public CustomDashStyleComboBox(string _actionName, Type _type)
        {
            this.Name = _actionName;
            this._type = _type;
            RefreshComboItem();
        }

        protected virtual void RefreshComboItem()
        {
            using (Pen pen = new Pen(Color.Black, 2f))
            {
                foreach (string _enumName in Enum.GetNames(_type))
                {
                    ComboItem ci = new ComboItem();
                    ci.Tag = _enumName;
                    Bitmap map = new Bitmap(40, 16);
                    using (Graphics graph = Graphics.FromImage(map))
                    {
                        pen.DashStyle = (DashStyle)ControlsUnity.GetNameToEnum(_enumName, _type);
                        if (_enumName == "Custom")
                        {
                            pen.DashPattern = new float[] { 5, 5 };
                        }
                        graph.DrawLine(pen, 0, 8, 40, 8);
                        ci.Image = map;
                        this.Items.Add(ci);
                    }
                }
            }
        }

        protected override void OnSelectedValueChanged(EventArgs e)
        {
            if (this.Parent is Form)
            {
                this.Tag = ((ComboItem)this.SelectedItem).Tag;
                ((Form)this.Parent).Refresh();
            }
        }

        #region IToolAction 成员
        //private ToolAction _action;
        //public ToolAction Action
        //{
        //    get { return _action; }
        //    set { _action = value; }
        //}

        public string ActionName
        {
            get { return this.Name; }
        }

        public void UpdateAction()
        {
            //this.Enabled = this.Action.IsEnable();
            ////this.Visible = this.Action.IsVisible();
            //if (this.Enabled && this.Visible)
            //{
            //    string text = this.Action.GetStrParam1();
            //    int i = 0;
            //    foreach (ComboItem _value in this.Items)
            //    {
            //        if (text == _value.Tag.ToString())
            //        {
            //            this.SelectedIndex = i;
            //            break;
            //        }
            //        i++;
            //    }
            //}
            //else
            //{
            //    this.SelectedIndex = -1;
            //}
        }

        public void SetDashStyle(string enumName)
        {
            int i = 0;
            foreach (ComboItem _value in this.Items)
            {
                if (enumName == _value.Tag.ToString())
                {
                    this.SelectedIndex = i;
                    return;
                }
                i++;
            }
            this.SelectedIndex = 0;
        }
        #endregion
    }
}
